#include <stdio.h>
#include <algorithm>
#include <functional>

#define MAX_N 30

static bool unit_digit_comparer(int a, int b)
{
    return (a % 10 < b % 10) || (a % 10 == b % 10 && a < b);
}

static void solve()
{
    int s1[MAX_N], s2[MAX_N];   /* score sequences */
    int n1, n2;                 /* number of elements in each sequence */

    /* Read score sequences. */
    scanf("%d %d", &n1, &n2);
    for (int i = 0; i < n1; i++)
        scanf("%d", &s1[i]);
    for (int i = 0; i < n2; i++)
        scanf("%d", &s2[i]);

    /* Sort the sequences in descending order. */
    std::sort(s1 + 0, s1 + n1, std::greater<int>());
    std::sort(s2 + 0, s2 + n2, std::greater<int>());

    /* Remove duplicate elements. */
    n1 = std::unique(s1 + 0, s1 + n1) - s1;
    n2 = std::unique(s2 + 0, s2 + n2) - s2;

    /* Find the longest consecutive common subsequence. */
    int i1 = 0, i2 = 0;
    int start = -1, length = 0;
    int current_length = 0;
    while (i1 < n1 && i2 < n2)
    {
        if (s1[i1] == s2[i2])
        {
            current_length++;
            i1++;
            i2++;
            continue;
        }
        if (current_length > length)
        {
            length = current_length;
            start = i1 - current_length;
        }
        current_length = 0;
        if (s1[i1] > s2[i2])
            i1++;
        else
            i2++;
    }
    if (current_length > length)
    {
        length = current_length;
        start = i1 - current_length;
    }

    /* Print longest consecutive subsequence. */
    if (length == 0)
    {
        printf("NONE\n");
        return;
    }
    for (int i = 0; i < length; i++)
        printf("%d%c", s1[start + i], (i == length - 1)? '\n' : ' ');
    
    /* Sort the subsequence by unit digit and print the result. */
    std::sort(s1 + start, s1 + start + length, unit_digit_comparer);
    for (int i = 0; i < length; i++)
        printf("%d%c", s1[start + i], (i == length - 1)? '\n' : ' ');
}

int main()
{
    int num_cases;
    scanf("%d", &num_cases);
    while (num_cases--)
        solve();
    return 0;
}